﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Product
{
    class ExecuteSQL
    {
        public string Server { get; set; }
        public string Data { get; set; }
        public string User { get; set; }
        public string Passwword { get; set; }

        private SqlConnection conn;
        private DataTable dTable;
        public DataTable DTable { get; set; }
        private SqlDataAdapter dAdapter;
        public SqlDataAdapter DAdapter { get; set; }

        public void ConectDb()
        {
            try
            {
                StreamReader read = new StreamReader("RRCConfig.ini");
                this.Server = (read.ReadLine().Split(':')[1]);
                this.Data = (read.ReadLine().Split(':')[1]);
                this.User = (read.ReadLine().Split(':')[1]);
                string giaima = (read.ReadLine().Split(':')[1]);

                this.Passwword = GiaiMa(giaima, "123");
                read.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message + "");
            }
        }
        public SqlConnection GetCon()
        {
            ConectDb();
            return new SqlConnection("data source=" + this.Server + ";initial catalog = " + this.Data + ";user id = " + this.User + ";password = " + this.Passwword + ";");
        }
        public static void writefile(string server, string db, string user, string pass)
        {
            StreamWriter write = new StreamWriter("info.ini");
            write.WriteLine("SV:" + server);
            write.WriteLine("DB:" + db);
            write.WriteLine("Us:" + user);
            write.WriteLine("Pw:" + pass);
            write.Close();
        }
        public string MaHoa(string strEnCrypt, string key)
        {
            try
            {
                byte[] keyArr;
                byte[] EnCryptArr = UTF8Encoding.UTF8.GetBytes(strEnCrypt);
                MD5CryptoServiceProvider MD5Hash = new MD5CryptoServiceProvider();
                keyArr = MD5Hash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider();
                tripDes.Key = keyArr;
                tripDes.Mode = CipherMode.ECB;
                tripDes.Padding = PaddingMode.PKCS7;
                ICryptoTransform transform = tripDes.CreateEncryptor();
                byte[] arrResult = transform.TransformFinalBlock(EnCryptArr, 0, EnCryptArr.Length);
                return Convert.ToBase64String(arrResult, 0, arrResult.Length);
            }
            catch (Exception ex) { }
            return "";
        }
        public string GiaiMa(string strDecypt, string key)
        {
            try
            {
                byte[] keyArr;
                byte[] DeCryptArr = Convert.FromBase64String(strDecypt);
                MD5CryptoServiceProvider MD5Hash = new MD5CryptoServiceProvider();
                keyArr = MD5Hash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
                TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider();
                tripDes.Key = keyArr;
                tripDes.Mode = CipherMode.ECB;
                tripDes.Padding = PaddingMode.PKCS7;
                ICryptoTransform transform = tripDes.CreateDecryptor();
                byte[] arrResult = transform.TransformFinalBlock(DeCryptArr, 0, DeCryptArr.Length);
                return UTF8Encoding.UTF8.GetString(arrResult);
            }
            catch (Exception ex) { }
            return "";
        }

        public DataTable GetAll(string sql, bool setDataProperties)
        {
            conn = GetCon();
            if (setDataProperties)
            {
                dAdapter = new SqlDataAdapter(sql, conn);
                dTable = new DataTable();
                dAdapter.Fill(dTable);
                return dTable;
            }
            else
            {
                SqlDataAdapter dataAdap = new SqlDataAdapter(sql, conn);
                DataTable dataTable = new DataTable();
                dataAdap.Fill(dataTable);
                return dataTable;
            }
        }

        public void DoInsert(string sql)
        {
            conn = GetCon();
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.ExecuteNonQuery();
        }
        public void DoDelete(string sql)
        {
            conn = GetCon();
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.ExecuteNonQuery();
        }
        public void DoUpdate(string sql)
        {
            conn = GetCon();
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.ExecuteNonQuery();
        }
    }
}
